﻿using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SalesPages.Infrastructure
{
    public partial interface IRedisCacheService
    {
        #region Redis String类型操作
        /// <summary>
        /// Redis String类型 新增一条记录
        /// </summary>
        /// <typeparam name="T">generic refrence type</typeparam>
        /// <param name="key">unique key of value</param>
        /// <param name="value">value of key of type object</param>
        /// <param name="expiresAt">time span of expiration</param>
        /// <param name= "when">枚举类型</param>
        /// <param name="commandFlags"></param>
        /// <returns>true or false</returns>
        bool StringSet<T>(string key, object value, TimeSpan? expiry = default(TimeSpan?), When when = When.Always, CommandFlags commandFlags = CommandFlags.None) where T : class;

        /// <summary>
        /// Redis String类型 新增一条记录
        /// </summary>
        /// <typeparam name="T">generic refrence type</typeparam>
        /// <param name="key">unique key of value</param>
        /// <param name="value">value of key of type object</param>
        /// <param name="expiresAt">time span of expiration</param>
        /// <param name= "when">枚举类型</param>
        /// <param name="commandFlags"></param>
        /// <returns>true or false</returns>
        bool StringSet<T>(string key, T value, TimeSpan? expiry = default(TimeSpan?), When when = When.Always, CommandFlags commandFlags = CommandFlags.None) where T : class;

        /// <summary>
        /// 更新时应使用此方法，代码更可读。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="expiresAt"></param>
        /// <param name="when"></param>
        /// <param name="commandFlags"></param>
        /// <returns></returns>
        bool StringUpdate<T>(string key, T value, TimeSpan expiresAt, When when = When.Always, CommandFlags commandFlags = CommandFlags.None) where T : class;

        /// <summary>
        /// Redis String类型  Get
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="commandFlags"></param>
        /// <returns>T</returns>
        T StringGet<T>(string key, CommandFlags commandFlags = CommandFlags.None) where T : class;

        /// <summary>
        /// Redis String数据类型 获取指定key中字符串长度
        /// </summary>
        /// <param name="key"></param>
        /// <param name="commandFlags"></param>
        /// <returns></returns>
        long StringLength(string key, CommandFlags commandFlags = CommandFlags.None);

        /// <summary>
        ///  Redis String数据类型  返回拼接后总长度
        /// </summary>
        /// <param name="key"></param>
        /// <param name="appendVal"></param>
        /// <param name="commandFlags"></param>
        /// <returns>总长度</returns>
        long StringAppend(string key, string appendVal, CommandFlags commandFlags = CommandFlags.None);

        /// <summary>
        /// 设置新值并且返回旧值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="newVal"></param>
        /// <param name="commandFlags"></param>
        /// <returns>OldVal</returns>
        string StringGetAndSet(string key, string newVal, CommandFlags commandFlags = CommandFlags.None);

        /// <summary>
        /// 为数字增长val
        /// </summary>
        /// <param name="key"></param>
        /// <param name="val"></param>
        /// <param name="commandFlags"></param>
        /// <returns>增长后的值</returns>
        double StringIncrement(string key, double val, CommandFlags commandFlags = CommandFlags.None);

        void StringSetBatch(IList<Tuple<string, object>> caches, TimeSpan? expiresAt = default(TimeSpan?), When when = When.Always, CommandFlags commandFlags = CommandFlags.None);


        #endregion


        #region Redis各数据类型公用

        /// <summary>
        /// Redis中是否存在指定Key
        /// </summary>
        /// <param name="key"></param>
        /// <param name="commandFlags"></param>
        /// <returns></returns>
        bool KeyExists(string key, CommandFlags commandFlags = CommandFlags.None);

        /// <summary>
        /// 从Redis中移除键
        /// </summary>
        /// <param name="key"></param>
        /// <param name="commandFlags"></param>
        /// <returns></returns>
        bool KeyRemove(string key, CommandFlags commandFlags = CommandFlags.None);

        /// <summary>
        /// 从Redis中移除多个键
        /// </summary>
        /// <param name="keys"></param>
        /// <param name="commandFlags"></param>
        /// <returns></returns>
        void KeyRemove(RedisKey[] keys, CommandFlags commandFlags = CommandFlags.None);

        /// <summary>
        /// Dispose DB connection 释放DB相关链接
        /// </summary>
        void DbConnectionStop();
        #endregion
    }
}
